package bookdata;

import dbbook.DbBook;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Iterator;
import java.util.*;
import java.util.Scanner;

public class Bookff {
//    LinkedList linkedList=new LinkedList();
//    LinkedList rebookList=new LinkedList();
    List<BookData> list = new LinkedList<>();
    List<ReBook> rebookList = new LinkedList<>();

    public int differentDaysByMillisecond(int year, int month, int day)
    {
        LocalDate of = LocalDate.of(year, month, day);
        long l = of.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        int days = (int) ((System.currentTimeMillis()-l) / (1000*3600*24));
        return days;
    }

//
    public void showBook() {
        for (int i =0;i< list.size();i++) {
            System.out.println(list.get(i).toString());
        }

    }


    public void outDate() {
        for (int i =0;i< rebookList.size();i++) {
            int t = differentDaysByMillisecond(rebookList.get(i).getYear(),rebookList.get(i).getMouth(),rebookList.get(i).getDay());
            if (t>15) {
                System.out.println("读者号:"+rebookList.get(i).getReaderid()+"  姓名:"+rebookList.get(i).getReadername() + "借书"+rebookList.get(i).getBookid()+rebookList.get(i).getBookname()+" 超期" + (t-15)+"天，罚款：" + (t-15)*0.8);
            }else{
                System.out.println("未超期");
            }
        }
    }

    public void read() {
        List<String> stringList = new DbBook().readFile("D:\\book.txt");
        for(String str : stringList) {
            String[] split = str.trim().split(" ");
            if(split.length>3) {
                BookData bookData = new BookData(split[0], split[1], split[2], split[3]);
                list.add(bookData);
            }
        }
    }
    public void save() {
        try{

            String str = "";
            for (int i =0;i < list.size();i ++) {
                str += list.get(i).getBookid()+","+list.get(i).getBookname()+ "," + list.get(i).getAuthor() +  "," + list.get(i).getPress()+";\r\n";
            }
            File file =new File("D:\\book.txt");
            if(!file.exists()){
                file.createNewFile();

            }
                new DbBook().writeInFile(file, str);

        }catch(IOException e){
            e.printStackTrace();
        }

    }
    public void Bookadd() {
        System.out.println("请输入图书书号 书名 作者 出版社");
        Scanner scanner = new Scanner(System.in);
        String bookid = scanner.next();
        String bookname = scanner.next();
        String author = scanner.next();
        String press = scanner.next();
        BookData bookData = new BookData(bookid, bookname, author, press);
        for (int i =0;i< list.size();i++) {
            if (bookData.equals(list.get(i))) {
                System.out.println("已存在该书！");
                return;
            }
        }
        list.add(bookData);
        System.out.println("增加成功");
    }

    public void Bookdel() {
        System.out.println("请输入图书书名 作者");
        Scanner scanner = new Scanner(System.in);
        //String bookid= scanner.next();
        String bookname=scanner.next();
        String author=scanner.next();
        Iterator<BookData> it = list.iterator();
        while(it.hasNext()){
            BookData bookData = it.next();
            if(bookData.getBookname().equals(bookname)&&bookData.getAuthor().equals(author)){
                it.remove();
                System.out.println("删除成功");
                break;
            }else{
                System.out.println("无此书");
                   break;
            }
        }
    }

    public void showAll() {
        for (int i =0;i < rebookList.size(); i++) {
            System.out.println(rebookList.get(i).toString());
        }
    }



    public void Bookfind() {
        System.out.println("1.按书号搜索");
        System.out.println("2.按书名搜索");
        System.out.println("3.按作者搜索");
        System.out.println("4.按出版社搜索");
        System.out.println("5.多重查找输入书名 作者");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println("请输入搜索词");
        String str = scanner.next();
        String ss=scanner.next();
        for (int i = 0; i < list.size(); i++) {
            switch (n) {
                case 1:
                    if (list.get(i).getBookid().equals(str)) {
                        System.out.println(list.get(i));
                    }
                    break;
                case 2:
                    if (list.get(i).getBookname().equals(str)) {
                        System.out.println(list.get(i));
                    }
                    break;
                case 3:
                    if (list.get(i).getAuthor().equals(str)) {
                        System.out.println(list.get(i));
                    }
                    break;
                case 4:
                    if (list.get(i).getPress().equals(str)) {
                        System.out.println(list.get(i));
                    }
                    break;
                case 5:

                    if(list.get(i).getBookname().equals(str)){

                     if(list.get(i).getAuthor().equals(ss)){
                         System.out.println(list.get(i));
                     }

                    }
            }
        }
    }

    public void Bookborrow() {
        System.out.println("请输入书号 书名 读者号 读者姓名 借书年月日");
        Scanner scanner = new Scanner(System.in);
        String bookid= scanner.next();
        String readerid= scanner.next();
        String readername=scanner.next();
        String  bookname=scanner.next();
        int year = scanner.nextInt();
        int mouth = scanner.nextInt();
        int day= scanner.nextInt();
        ReBook bb = new ReBook(bookid, readerid,readername,bookname, year, mouth, day);
        rebookList.add(bb);
    }

    public void Bookback() {
        System.out.println("请输入读者号 书号");
        Scanner scanner = new Scanner(System.in);
        String readerid = scanner.next();
        String bookid= scanner.next();
        Iterator<ReBook> it = rebookList.iterator();
        while(it.hasNext()){
            ReBook book = it.next();
            if(book.getBookid().equals(bookid) && book.getReaderid().equals(bookid)){
                it.remove();
                break;
            }
        }
    }

}
